<?php
/**
 * Created by dailinlin.
 * Date: 2019/12/2 17:12
 * for:
 */


namespace app\common\controller;


use app\admin\model\Area;
use app\admin\model\Hospital;
use app\admin\model\School;
use app\admin\model\Student as StudentModel;
use app\common\model\Ent;
use app\common\model\Eyes;
use app\common\model\Fluoroscopy;
use app\common\model\Liver;
use app\common\model\Medicine;
use app\common\model\Stomatology;
use app\common\model\Surgery;
use auth\Auth;
use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
use think\Db;
use think\facade\Config;
use think\facade\View;
use think\helper\Str;

class BaseAdmin extends Base
{

    protected $auth_uid;    //后台管理员登录id

    public function initialize(){
        $this->checkAuth();
    }


    /**
     * 权限登录检查
     * @param null $action
     * @param null $controller
     * @param null $module
     * @param array $route
     * @throws \Exception
     */
    protected function checkAuth($action = null,$controller = null,$module = null,array $route = []){
        if($module === null) $module = app('http')->getName();
        if($controller === null) $controller = $this->request->controller();
        if($action === null) $action = $this->request->action();

        //拼接路由
        $url = Str::lower($module.'/'.$controller.'/'.$action);

        //权限监测
        $auth = Auth::instance();
        $config = Config::get('login_admin');

        //无需认证模块
        $modules = explode(',',$config['not_auth_module']);
        $check = in_array(Str::lower($module.'/'.$controller),$modules);

        //是否登录否则人让用户登录
        $this->auth_uid = session($config['auth_uid']);
        if (!$this->auth_uid&&!$check){
            $this->redirect(url($config['user_auth_gateway']));
        }

        if(!$check&&!$auth->check($url, $this->auth_uid)){
            if($this->request->isAjax()){
                $this->error('对不起您没有该操作权限!');
            }else{
                echo $this->fetch('template@layout/access_denied');die;
            }
        }

        //后台操作日志
        admin_log($this->auth_uid?:0);
    }

    /**
     * 是否登录否则跳转登录页面
     */
    protected function checkLogin(){
        $config = Config::get('login_admin');
        $auth_uid = session($config['auth_uid']);
        if (!$auth_uid){
           $this->redirect(url($config['user_auth_gateway']));
        }
    }


    /**
     * 模板变量赋值
     * @param $name
     * @param null $value
     */
    protected function assign($name, $value = null){
        View::assign($name,$value);
    }

    /**
     * 解析和获取模板内容 用于输出
     * @param string $template
     * @param array $vars
     * @return string
     * @throws \Exception
     */
    protected function fetch(string $template = '', array $vars = []){
        return View::fetch($template, $vars);
    }


    /**
     * 返回表格数据
     * @param $data
     * @param $total
     * @return array
     */
    protected function tableJson($data, $total)
    {
        $json = [
            'code' => 0,
            'msg' => '',
            'data' => $data,
        ];
        if ($total) {
            $json = array_merge($json, ['count' => $total]);
        }
        return $json;
    }

	/**
     * 表格数据导出
     * @param array $data  数据
     * @param array $filed 导出字段
     * @param int $total 总书记
     * @param string $template 模板名称
     * @param string $filename 文件名称名称
     * @return array
     */
    protected function tableExport($data,$filed,$total,$template,$filename = '导出数据'){
        $template = $this->request->get('xls',$template);
        $save_path = Excel::saveTemplate($data,$filed,$template,$filename.'-'.date('YmdHis'));

        //计算进度
        $limit = $this->request->get('limit');
        $page = $this->request->get('page');
        $rate = $page*$limit/$total*100;
        $percent = $rate>100?100:$rate;
        $data = [
            'download' => $percent>=100?$this->request->domain().'/'.$save_path:'',
            'path' => $save_path,
            'percent' => $percent
        ];
        return $this->tableJson($data,$total);
    }

    /**
     * 配合withSearch搜索器使用
     * @param $data
     * @return array
     */
    protected function searchFields($data){
        unset($data['limit']);
        $data = array_keys($data);
        return $data;
    }

    /**
     * 配合withSearch搜索器使用
     * @param $data
     * @return array
     */
    protected function searchData($data){
        unset($data['limit']);
        return $data;
    }

}